package com.wash.shoes.domain.dy.store;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

public class Store {
    private static final ConcurrentHashMap<String, UserSession> cache = new ConcurrentHashMap<String, UserSession>();

    public static UserSession GetCache(String openid) {
        return cache.get(openid);
    }


    // 这里token仅使用uuid来做示意，需要用户根据业务需要自行覆盖填充
    public static String GenToken(){
        return UUID.randomUUID().toString();
    }

    public static String SetCache(String token, String openid, String unionid, String data, String sessionKey) {
        UserSession userData = cache.get(token);
        if (userData == null) {
            userData = new UserSession();
            userData.setData(data);
            userData.setOpenid(openid);
            userData.setUnionid(unionid);
            userData.setSessionKey(sessionKey);
            String newToken = GenToken();
            userData.setToken(newToken);
            cache.put(newToken, userData);
            return newToken;
        } else {
            userData.setOpenid(openid);
            userData.setUnionid(unionid);
            userData.setData(data);
            userData.setSessionKey(sessionKey);
            return token;
        }
    }
}
